package com.monkey.web.platform.controller;

import com.monkey.core.plugin.report.excel.ExcelData;
import com.monkey.core.plugin.report.pdf.PdfData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.view.excel.AppExcelView;
import org.springframework.web.servlet.view.pdf.AppPdfView;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.util.*;

/**
 * Created by silentwu on 2015/6/26.
 */
@Controller
@RequestMapping(value = "/report/")
public class ReportController {


    @RequestMapping(value = "excel-test1", method = RequestMethod.GET)
    public String excel(Model model) {
        ExcelData excelData = new ExcelData();
        excelData.addParameter("reportTitle", "test");
        excelData.addParameter("jbr", "SilentWu");
        excelData.addParameter("amount", 100);
        excelData.addParameter("date", new Date());
        excelData.addVariable("count", 50);
//        excelData.setFields(loadProjects());
        excelData.setDownloadFileName("test.xls");

        model.addAttribute(AppExcelView.EXCEL_DATA_MODEL_KEY, excelData);
        return "excel:report/excel/excel-report";
    }

    @RequestMapping(value = "excel-test2", method = RequestMethod.GET)
    public ExcelData excel2() {
        ExcelData excelData = new ExcelData();
        excelData.addParameter("reportTitle", "test");
        excelData.addParameter("jbr", "SilentWu");
        excelData.addParameter("amount", 100);
        excelData.addParameter("date", new Date());
        excelData.addVariable("count", 50);
//        excelData.setFields(loadProjects());
        excelData.setDownloadFileName("test.xls");
        excelData.setTemplatePath("report/excel/excel-report");
        return excelData;
    }


    @RequestMapping(value = "pdf-test1", method = RequestMethod.GET)
    public String pdf(Model model, HttpSession session) throws FileNotFoundException {
        PdfData pdfData = new PdfData();
        pdfData.addParameter("reportTitle", "test项目表");
        pdfData.addParameter("jbr", "SilentWu");
        pdfData.addParameter("jbsj", new Date());

        pdfData.addParameter("SUBREPORT_DIR", WebUtils.getRealPath(session.getServletContext(), "/") + "report/pdf/report1.jasper");
        //添加子报表参数
        Map<String, Object> subParams = new HashMap<>();
        subParams.put("subTitle", "subTitle....");
        pdfData.addParameter("subParameters", subParams);

//        pdfData.addParameter("subFields", loadProjects());

//        pdfData.addFields(loadProjects());
        pdfData.setDownloadFileName("test项目表.pdf");

        model.addAttribute(AppPdfView.PDF_MAIN_DATA_MODEL_KEY, pdfData);
        return "pdf:report/pdf/report.jasper";
    }


    @RequestMapping(value = "pdf-test2", method = RequestMethod.GET)
    public PdfData pdf2(HttpSession session) throws FileNotFoundException {
        PdfData pdfData = new PdfData();
        pdfData.addParameter("reportTitle", "test项目表");
        pdfData.addParameter("jbr", "SilentWu");
        pdfData.addParameter("jbsj", new Date());

        pdfData.addParameter("SUBREPORT_DIR", WebUtils.getRealPath(session.getServletContext(), "/") + "report/pdf/report1.jasper");
        //添加子报表参数
        Map<String, Object> subParams = new HashMap<>();
        subParams.put("subTitle", "subTitle....");
        pdfData.addParameter("subParameters", subParams);

//        pdfData.addParameter("subFields", loadProjects());

//        pdfData.addFields(loadProjects());
        pdfData.setReportURL("report/pdf/report.jasper");
        pdfData.setDownloadFileName("test项目表.pdf");

        return pdfData;
    }

//    private List<Project> loadProjects() {
//        List<Project> projects = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            Project project = new Project();
//            project.setProjectId(i);
//            project.setProjectName("test-" + i);
//            projects.add(project);
//        }
//        return projects;
//    }

}
